package com.tensquare.test.base.service;

import com.tensquare.test.base.dao.LabelDao;
import com.tensquare.test.base.pojo.Label;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class LableService {
    @Autowired
    private LabelDao lableDao;
    @Autowired
    private IdWorker idWorker ;
    public List<Label> findAll() {
       return lableDao.findAll();
    }

    public Label findById(String id){
        return lableDao.findById(id).get();

    }

    public void save( Label label) {
        //save有id是更新，没id是保存
        label.setId(idWorker.nextId()+"");
        lableDao.save(label);
    }

    public void update(  Label label) {
      lableDao.save(label);
    }


    public void deleteById(String labelId) {
    lableDao.deleteById(labelId);
    }

    public List serach(Label label) {
        return  lableDao.findAll(new Specification<Label>() {
            List<Predicate> list = new ArrayList();
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
              if (label.getLabelname()!=null&&!"".equals(label.getLabelname())){
                  Predicate predicate = criteriaBuilder.like(root.get("labelname").as(String.class), "%" + label.getLabelname() + "%");
                    list.add(predicate)  ;

              }
                if (label.getState()!=null&&!"".equals(label.getState())){
                    Predicate predicate = criteriaBuilder.like(root.get("state").as(String.class),  label.getState() );
                    list.add(predicate)  ;

                }

                Predicate[] parr=new  Predicate[list.size()];
                parr =   list.toArray(parr);
                return criteriaBuilder.and(parr);
            }
        });

    }


    public Page<Label> pageQuery(int page, int size, Label label) {
        Pageable pageable = PageRequest.of(page-1,size);
        return lableDao.findAll(new Specification<Label>() {
            List<Predicate> list = new ArrayList();
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                if (label.getLabelname()!=null&&!"".equals(label.getLabelname())){
                    Predicate predicate = criteriaBuilder.like(root.get("labelname").as(String.class), "%" + label.getLabelname() + "%");
                    list.add(predicate)  ;

                }
                if (label.getState()!=null&&!"".equals(label.getState())){
                    Predicate predicate = criteriaBuilder.like(root.get("state").as(String.class),  label.getState() );
                    list.add(predicate)  ;

                }

                Predicate[] parr=new  Predicate[list.size()];
                parr =   list.toArray(parr);
                return criteriaBuilder.and(parr);
            }
        },pageable);

    }
}
